﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CIMDICT;

namespace WBEMINFO
{
    public partial class _Default : System.Web.UI.Page
    {
        private const char            SEPERATOR            = '.';
        private const string          LIST_FILE            = "hello.lst";
        private static CIMRepo        CIM_Repo             =  null;
        private const string          BASELINK             = "http://linuxqa.chn.hp.com:7997/wbeminfo";

        protected Collection<CIMClass>  obj_result_classes         = null;
        protected CIMClass              obj_selected_class         = null;
        protected CIMClass              obj_selected_class_of_prop = null;
        protected CIMProp               obj_selected_prop          = null;
        private string                ipt_search_text      = null;
        private string                ipt_selected_class   = null;
        private string                ipt_selected_prop    = null;
        private string                ipt_selected_class_of_prop = null;

        private static void addListItem(ListBox list, string name, string value)
        {
            ListItem li = new ListItem();
            li.Text = name;
            li.Value = value;
            if (list.Items.IndexOf(li) < 0)
            {
                list.Items.Add(li);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check if a new list needs to rebuild
            string lf = Server.MapPath(LIST_FILE);
            if (!System.IO.File.Exists(lf))
            {
                CIM_Repo = null;
                System.Diagnostics.Process cmd = System.Diagnostics.Process.Start("cimtoolcmd.exe", @"" + LIST_FILE);
                cmd.WaitForExit();
            }
            if (CIM_Repo == null)
            {
                CIM_Repo = new CIMRepo(lf);
            }
        }

        private bool containRequestString(string reqStr)
        {
            foreach (string key in this.Request.Params.AllKeys)
            {
                if (key.Equals(reqStr)) return true;
            }
            return false;
        }

        private void addHiddenElement( string name, string value )
        {
            Response.Write("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\" />");
        }

        private bool isResetAllValue()
        {
            return this.containRequestString("SearchButton");
        }

        protected void parseSessionValue( string name, string[] alertNames, out string value)
        {
            value = null;
            if (string.IsNullOrEmpty(name) || alertNames.Length < 1) return;

            //Check if the request name exists
            if (this.containRequestString(name)) value = this.Request.Params[name];
            //If this is a new search, reset all value
            if (this.isResetAllValue()) return;

            //If not exist, check the alterNames
            foreach (string alterName in alertNames)
            {
                if (!string.IsNullOrEmpty(value)) break;
                if (string.IsNullOrEmpty(alterName) || !this.containRequestString(alterName)) continue;
                value = this.Request.Params[alterName];
            }
            if (string.IsNullOrEmpty(value)) return;

            //Write the alterName, value to webpage for further use
            Response.Write("<input type=\"hidden\" name=\"" + alertNames.Last() + "\" value=\"" + value + "\" />");
        }

        public void Handle_Query()
        {
            this.parseSessionValue("ClassNameInput", new string[]{"LastSearchInput"}, out this.ipt_search_text);

            this.parseSessionValue("ClassListBox", new string[] { "ClassInheritanceBox", "LastClassSelect" }, out this.ipt_selected_class);

            string value = null;
            this.parseSessionValue("ClassPropListBox", new string[] { "LastPropSelect" }, out value);
            if (!string.IsNullOrEmpty(value))
            { 
                string[] parts = value.Split(SEPERATOR);
                if (parts.Length == 2)
                {
                    this.ipt_selected_class_of_prop = parts[0];
                    this.ipt_selected_prop = parts[1];
                }
                else
                {
                    this.ipt_selected_class_of_prop = value;
                    this.ipt_selected_prop = null;
                }
            }

            this.obj_selected_class = null;
            if (!string.IsNullOrEmpty(this.ipt_selected_class))
            {
                this.obj_selected_class = CIM_Repo.getClass(this.ipt_selected_class);
            }

            this.obj_selected_class_of_prop = null;
            if (!string.IsNullOrEmpty(this.ipt_selected_class_of_prop))
            {
                this.obj_selected_class_of_prop = CIM_Repo.getClass(this.ipt_selected_class_of_prop);
            }

            if (this.obj_selected_class_of_prop != null && this.ipt_selected_prop != null)
            {
                this.obj_selected_prop = this.obj_selected_class_of_prop.getProp(this.ipt_selected_prop.ToLower());
            }
            return;
        }


        public void Fill_Class_List()
        {
            if (string.IsNullOrEmpty(this.ipt_search_text)) return;

            this.obj_result_classes = CIM_Repo.query(this.ipt_search_text);

            if (this.obj_result_classes != null)
            {
                foreach (CIMClass cim in this.obj_result_classes)
                {
                    addListItem(ClassListBox, cim.name, cim.name);
                }
            }
        }

        public void Fill_Class_Prop_List()
        {
            if (!string.IsNullOrEmpty(this.ipt_selected_class) && this.obj_selected_class != null)
            {
                CIMClass cim = this.obj_selected_class;
                while (cim != null)
                {
                    addListItem(ClassPropListBox, cim.name, cim.name);
                    foreach (CIMProp prop in cim.props)
                    {
                        addListItem(ClassPropListBox, "----"+prop.name, cim.name + SEPERATOR + prop.name);
                    }
                    cim = cim.baseClass;
                }
            }
        }

        public void Fill_Class_Inheritance()
        {
            if (!string.IsNullOrEmpty(this.ipt_selected_class) && this.obj_selected_class != null)
            {
                CIMClass cim = this.obj_selected_class;
                List<string> names = new List<string>();
                while (cim != null)
                {
                    names.Add(cim.name);
                    cim = cim.baseClass;
                }
                names.Reverse();

                string preffix = "";
                foreach (string name in names)
                {
                    addListItem(ClassInheritanceBox, preffix + name, name);
                    preffix += "--";
                }
            }
        }

        public void Print_File_Link()
        {
            if (this.obj_selected_class != null)
            {
                string[] parts = this.obj_selected_class.fileName.Split('\\');
                string name = parts[parts.Length - 1];

                string link_name = "";
                const string SEP = "\\mofs";
                int pos = obj_selected_class.fileName.IndexOf(SEP);
                if ( pos >= 0 )
                {
                    string subname = this.obj_selected_class.fileName.Substring( pos);
                    subname = subname.Replace('\\', '/');
                    link_name = BASELINK + subname;
                }
                Response.Write("<a href=\""+link_name+"\" target=\"_blank\">"+name+"</a>");
            }
        }
        public void Print_Class_Define()
        {
            //Response.Write("Test");
        }

        public void Print_Class_Prop_Define()
        {
            //Response.Write("Test");
        }

    }
}
